﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Common;


namespace DataAccessLayer
{
    public class Master_UnitCode_Types_DB
    {
        #region Declarations
        
        Database objDatabase = null;
        DbCommand ObjCommand = null;
        MethodResult ObjMethodResult = null;

        public const string ADDUNITCODEMASTER = "AddUnitCodeMasterItem";
        public const string UPDATEUNITCODEMASTER = "UpdateUnitCodeMasterItem";
        public const string DELETEUNITCODEMASTER = "DeleteUnitCodeMasterItem";

        #endregion


        public Master_UnitCode_Types_DB()
        {
            // Used to connect to more than one database
            DatabaseProviderFactory Objfactory = new DatabaseProviderFactory();
            objDatabase = Objfactory.CreateDefault();
            ObjMethodResult = new MethodResult();
        }

        
        public MethodResult AddUnitCodeMaster(string unit_code, string desc, Boolean iscustomunit, string ref_unitcode, Int16 min_hours, Int16 max_hours, string status, string modified_by,out string result_Message)
        {
            ObjCommand = objDatabase.GetStoredProcCommand(ADDUNITCODEMASTER);

            objDatabase.AddInParameter(ObjCommand, "Unit_Code", DbType.String, unit_code);
            objDatabase.AddInParameter(ObjCommand, "Desc", DbType.String, desc);
            objDatabase.AddInParameter(ObjCommand, "IsCustomUnit", DbType.Boolean, iscustomunit);
            objDatabase.AddInParameter(ObjCommand, "Ref_UnitCode", DbType.String, ref_unitcode);
            objDatabase.AddInParameter(ObjCommand, "Min_Hours", DbType.Int16, min_hours);
            objDatabase.AddInParameter(ObjCommand, "Max_Hours", DbType.Int32, max_hours);
            objDatabase.AddInParameter(ObjCommand, "Status", DbType.Boolean, status);
            objDatabase.AddInParameter(ObjCommand, "Modified_By", DbType.String, modified_by);
            objDatabase.AddOutParameter(ObjCommand, "Return_Message", DbType.String, 100);

            short result = (short)objDatabase.ExecuteNonQuery(ObjCommand);

            result_Message = objDatabase.GetParameterValue(ObjCommand, "Return_Message").ToString();
            if (result < 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Warning, result_Message, null, result);
            else if (result > 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Success, result_Message, null, result);
            else
                return ObjMethodResult.CurrentMessageResult(MessageType.Information, result_Message, null, result);

        }

        public MethodResult UpdateUnitCodeMaster(string unit_code, string desc, Boolean iscustomunit, string ref_unitcode, Int16 min_hours, Int16 max_hours, string status, string modified_by, out string result_Message)
        {
            ObjCommand = objDatabase.GetStoredProcCommand(UPDATEUNITCODEMASTER);

            objDatabase.AddInParameter(ObjCommand, "Unit_Code", DbType.String, unit_code);
            objDatabase.AddInParameter(ObjCommand, "Desc", DbType.String, desc);
            objDatabase.AddInParameter(ObjCommand, "IsCustomUnit", DbType.Boolean, iscustomunit);
            objDatabase.AddInParameter(ObjCommand, "Ref_UnitCode", DbType.String, ref_unitcode);
            objDatabase.AddInParameter(ObjCommand, "Min_Hours", DbType.Int16, min_hours);
            objDatabase.AddInParameter(ObjCommand, "Max_Hours", DbType.Int16, max_hours);
            objDatabase.AddInParameter(ObjCommand, "Status", DbType.Boolean, status);
            objDatabase.AddInParameter(ObjCommand, "Modified_By", DbType.String, modified_by);
            objDatabase.AddOutParameter(ObjCommand, "Return_Message", DbType.String, 100);

            short result = (short)objDatabase.ExecuteNonQuery(ObjCommand);

            result_Message = objDatabase.GetParameterValue(ObjCommand, "Return_Message").ToString();
            if (result < 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Warning, result_Message, null, result);
            else if (result > 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Success, result_Message, null, result);
            else
                return ObjMethodResult.CurrentMessageResult(MessageType.Information, result_Message, null, result);

        }

        public MethodResult DeleteUnitCodeMaster(string unit_code,string modified_by, out string result_Message)
        {
            ObjCommand = objDatabase.GetStoredProcCommand(DELETEUNITCODEMASTER);

            objDatabase.AddInParameter(ObjCommand, "Unit_Code", DbType.String, unit_code);
            objDatabase.AddInParameter(ObjCommand, "Modified_By", DbType.String, modified_by);
            objDatabase.AddOutParameter(ObjCommand, "Return_Message", DbType.String, 100);

            short result = (short)objDatabase.ExecuteNonQuery(ObjCommand);

            result_Message = objDatabase.GetParameterValue(ObjCommand, "Return_Message").ToString();
            if (result < 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Warning, result_Message, null, result);
            else if (result > 0)
                return ObjMethodResult.CurrentMessageResult(MessageType.Success, result_Message, null, result);
            else
                return ObjMethodResult.CurrentMessageResult(MessageType.Information, result_Message, null, result);

        }


        public List<Master_UnitCode_Types> GetUnitCodeMasterItems(string unit_code, string mode)
        {
            string query = "SELECT * FROM Unit_Code_Master";

            if (mode == "SL")  //Retrive Records by Unit code
		    
                query = query + " where  Unit_Code = '" + unit_code + "'" ;
            
            else if (mode=="UL") //Retrive Records for dropdown
            
                query = query + " where IsCustomUnit = 0" ;

            var listusers = objDatabase.ExecuteSqlStringAccessor<Master_UnitCode_Types>(query);
            return listusers.ToList<Master_UnitCode_Types>();

        }



    }

     

}
